package ylsb.controller;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.Haocaijieguo;
import ylsb.model.Haocaiyinjin;
import ylsb.model.YltpJieguo;
import ylsb.model.YltpRenyuanfenpei;
import ylsb.service.HaocaijieguoService;
import ylsb.service.HaocaiyinjinService;
import ylsb.service.YltpjieguoService;
import ylsb.service.YltprenyuanfenpeiService;
import ylsb.support.CreateXLS;
import ylsb.support.JsonResult;
import ylsb.support.StringHandle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("haocaijieguo")
public class HaocaijieguoController {

    @Value("${userfilepath}")
    private String userfilepath;
    @Resource
    HaocaijieguoService ddService;
    @Resource
    HaocaiyinjinService ddHaocaiyinjinService;
    @Resource
    YltprenyuanfenpeiService ddYltprenyuanfenpeiService;

    public Logger log = LoggerFactory.getLogger(HaocaijieguoController.class);

    @RequestMapping("toupiao")
    public JsonResult<?> toupiao(@RequestParam("id") String ppdetailId,
                                 @RequestParam("pinzhong") String ppPinzhong,
                                 @RequestParam("pinpai") String ppPinpai,
                                 @RequestParam("userid") String ppuserId,
                                 @RequestParam("result") String ppresult){

        try {
            Haocaijieguo mmHaocaijieguo=new Haocaijieguo();
            if(ppresult.equals("1")) {
                if (ddService.checkCountByPinzhongandUserId(ppPinzhong, ppuserId) < 2) {
                    if (ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId) != null) {
                        String mmId = ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId).getId();
                        mmHaocaijieguo.setId(mmId);
                        mmHaocaijieguo.setResult(ppresult);
                        mmHaocaijieguo.setCreatetime(new Date());
                        return ddService.modify(mmHaocaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    } else {
                        mmHaocaijieguo.setId(UUID.randomUUID().toString());
                        mmHaocaijieguo.setDetailid(ppdetailId);
                        mmHaocaijieguo.setPinzhong(Integer.parseInt(ppPinzhong));
                        mmHaocaijieguo.setPinpai(ppPinpai);
                        mmHaocaijieguo.setUserid(ppuserId);
                        mmHaocaijieguo.setResult(ppresult);
                        mmHaocaijieguo.setCreatetime(new Date());
                        return ddService.add(mmHaocaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    }
                } else {
                    return JsonResult.getErrorResult("同一品种的品牌同意数量不能超过2条。");
                }
            }else{
                if (ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId) != null) {
                    String mmId = ddService.checkexistByUserId(ppPinzhong, ppPinpai, ppuserId).getId();
                    mmHaocaijieguo.setId(mmId);
                    mmHaocaijieguo.setResult(ppresult);
                    mmHaocaijieguo.setCreatetime(new Date());
                    return ddService.modify(mmHaocaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                } else {
                    mmHaocaijieguo.setId(UUID.randomUUID().toString());
                    mmHaocaijieguo.setDetailid(ppdetailId);
                    mmHaocaijieguo.setPinzhong(Integer.parseInt(ppPinzhong));
                    mmHaocaijieguo.setPinpai(ppPinpai);
                    mmHaocaijieguo.setUserid(ppuserId);
                    mmHaocaijieguo.setResult(ppresult);
                    mmHaocaijieguo.setCreatetime(new Date());
                    return ddService.add(mmHaocaijieguo) == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                }
            }

        }catch (Exception e) {
            e.printStackTrace();
            log.error("haocaijieguo/toupiao:error",e);
            return JsonResult.getErrorResult("haocaijieguo/toupiao:error " + e.getMessage());
        }
    }



    @RequestMapping("getHaocaiduibiaoResultCount")
    public JsonResult<?> getHaocaiduibiaoResultCount(@RequestParam("xiangmuid") String ppxiangmuId,
                                                     @RequestParam("userid") String ppuserId){
        try {
            Integer mmTongyishu=ddService.getHaocaiduibiaoResultCount(ppxiangmuId,ppuserId,"1");
            Integer mmButongyishu=ddService.getHaocaiduibiaoResultCount(ppxiangmuId,ppuserId,"0");
            Integer mmTotal=ddHaocaiyinjinService.getVoteCount(ppxiangmuId);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("tongyiShu", mmTongyishu);
            mmMap.put("butongyiShu", mmButongyishu);
            mmMap.put("total", mmTotal);
            return JsonResult.getSuccessResult(mmMap);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("haocaijieguo/getHaocaiduibiaoResultCount:error",e);
            return JsonResult.getErrorResult("haocaijieguo/getHaocaiduibiaoResultCount:error " + e.getMessage());
        }
    }

    @RequestMapping("huizongbiao")
    public void huizongbiao(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam("xiangmuid") String ppxiangmuid,
                              @RequestParam("xiangmuleixing") String ppxiangmuleixing){

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        List<Map<String,Object>> mmList = ddHaocaiyinjinService.findDaochuPinzhongAll(ppxiangmuid);
        List<Map<String,Object>> mmJieGuoList=ddService.findByxiangmuId(ppxiangmuid);
        List<YltpRenyuanfenpei> mmUserList=ddYltprenyuanfenpeiService.findUserByXiangmuId(ppxiangmuid);

       // double mm=(double) mmUserList.size()*((double)2/(double)3);
        double mmNumber=Math.ceil((double) mmUserList.size()*((double)2/(double)3));
        int mmPassNumber=(int)mmNumber;
        OutputStream os=null;
        WritableWorkbook wbook=null;

        try{
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            String wenjiantitle = "耗材表决结果汇总表"+ StringHandle.tiemString(); //文件名
            String tmptitle = "医用耗材引进表决结果"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC);
            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,500); //第一行高度
            wsheet.mergeCells(0, 0, 14, 0);  //合并单元格
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,15); //宽度
            wsheet.setColumnView(2,15); //宽度
            wsheet.setColumnView(3,10); //宽度
            wsheet.setColumnView(4,20); //宽度
            wsheet.setColumnView(5,10); //宽度
            wsheet.setColumnView(6,10); //宽度
            wsheet.setColumnView(7,10); //宽度
            wsheet.setColumnView(8,15); //宽度
            wsheet.setColumnView(9,10); //宽度
            wsheet.setColumnView(10,10); //宽度
            wsheet.setColumnView(11,10); //宽度
            wsheet.setColumnView(12,15); //宽度
            wsheet.setColumnView(13,15); //宽度
            wsheet.setColumnView(14,20); //宽度

            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);
            wsheet.addCell(new Label(0, 1, "序号",wcfFC));
            wsheet.addCell(new Label(1, 1, "品种",wcfFC));
            wsheet.addCell(new Label(2, 1, "品牌",wcfFC));
            wsheet.addCell(new Label(3, 1, "型号拆分",wcfFC));
            wsheet.addCell(new Label(4, 1, "平台编码",wcfFC));
            wsheet.addCell(new Label(5, 1, "注册证名称",wcfFC));
            wsheet.addCell(new Label(6, 1, "规格",wcfFC));
            wsheet.addCell(new Label(7, 1, "型号",wcfFC));
            wsheet.addCell(new Label(8, 1, "注册证号",wcfFC));
            wsheet.addCell(new Label(9, 1, "生产商",wcfFC));
            wsheet.addCell(new Label(10, 1, "计价单位",wcfFC));
            wsheet.addCell(new Label(11, 1, "平台情况",wcfFC));
            wsheet.addCell(new Label(12, 1, "平台单价（元）",wcfFC));
            wsheet.addCell(new Label(13, 1, "同意数／总票数",wcfFC));
            wsheet.addCell(new Label(14, 1, "结果",wcfFC));
            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count=mmList.size();
            int mmpinzhongKaishihang=2;
            int mmpinpaiKaishihang=2;
            int mmxinghaoKaishihang=2;

            for(int i=0;i<count;i++){
                Map<String,Object> mmPinzhongMap=mmList.get(i);

                String mmPinzhongOne = mmPinzhongMap.get("pinzhong") == null ? "" : mmPinzhongMap.get("pinzhong").toString();
                int mmHebingshu = 0;

                List<Map<String,Object>> mmPinpailist = ddHaocaiyinjinService.findPinpaiByPinzhong(mmPinzhongOne);
                if (mmPinpailist.size()>0){
                    mmHebingshu = mmPinpailist.size()-1;
                }
                List<Map<String,Object>> mmPinpaiFenlei = ddHaocaiyinjinService.findPinpaiFenlei(mmPinzhongOne);
                for (int j = 0; j < mmPinpaiFenlei.size(); j++) {
                    int s=0;
                    int mmPinpaiHebingshu = 0;
                    Map<String,Object> mmPinpaiMap = mmPinpaiFenlei.get(j);
                    if (mmPinpaiMap!=null){
                        String mmPinpaiOne = mmPinpaiMap.get("pinpai") == null ? "" : mmPinpaiMap.get("pinpai").toString();
                        List<Map<String,Object>> mmXinhaoList = ddHaocaiyinjinService.findXinghaoByPinpai(mmPinzhongOne,mmPinpaiOne);
                        if (mmXinhaoList.size()>0){
                            mmPinpaiHebingshu = mmXinhaoList.size()-1;
                        }
                        for (int k = 0; k < mmXinhaoList.size(); k++) {
                            Map<String, Object> mmXinhaoMap = mmXinhaoList.get(k);
                            if (mmXinhaoMap != null) {
                                String mmXuhao = mmXinhaoMap.get("ordernumber")==null ? "" :mmXinhaoMap.get("ordernumber").toString();
                                String mmPinzhong=mmXinhaoMap.get("pinzhong")==null ? "" :mmXinhaoMap.get("pinzhong").toString();
                                String mmpinpai=mmXinhaoMap.get("pinpai")==null ? "" :mmXinhaoMap.get("pinpai").toString();
                                String mmxinghaochaifen=mmXinhaoMap.get("xinghaochaifen")==null ? "" :mmXinhaoMap.get("xinghaochaifen").toString();
                                String mmpingtaibianma=mmXinhaoMap.get("pingtaibianma")==null ? "" :mmXinhaoMap.get("pingtaibianma").toString();
                                String mmzhucezhengmingcheng=mmXinhaoMap.get("zhucezhengmingcheng")==null ? "" :mmXinhaoMap.get("zhucezhengmingcheng").toString();
                                String mmguige=mmXinhaoMap.get("guige")==null ? "" :mmXinhaoMap.get("guige").toString();
                                String mmxinghao=mmXinhaoMap.get("xinghao")==null ? "" :mmXinhaoMap.get("xinghao").toString();
                                String mmzhucezhenghao=mmXinhaoMap.get("zhucezhenghao")==null ? "" :mmXinhaoMap.get("zhucezhenghao").toString();
                                String mmshengchanshang=mmXinhaoMap.get("shengchanshang")==null ? "" :mmXinhaoMap.get("shengchanshang").toString();
                                String mmjijiadanwei=mmXinhaoMap.get("jijiadanwei")==null ? "" :mmXinhaoMap.get("jijiadanwei").toString();
                                String mmpingtaiqingkuang=mmXinhaoMap.get("pingtaiqingkuang")==null ? "" :mmXinhaoMap.get("pingtaiqingkuang").toString();
                                String mmpingtaidanjia=mmXinhaoMap.get("pingtaidanjia")==null ? "" :mmXinhaoMap.get("pingtaidanjia").toString();
                                //String mmnanjingshiyibaoma=mmXinhaoMap.get("nanjingshiyibaoma")==null ? "" :mmXinhaoMap.get("nanjingshiyibaoma").toString();
                                //String mmgongnengyongtu=mmXinhaoMap.get("gongnengyongtu")==null ? "" :mmXinhaoMap.get("gongnengyongtu").toString();

                                List<Map<String, Object>> mmAgreeList=mmJieGuoList.stream()
                                        .filter(jieguo -> jieguo.get("xiangmuid").toString()
                                                .equals(ppxiangmuid) &&
                                                jieguo.get("pinzhong").toString()
                                                        .equals(mmPinzhong)&&
                                                jieguo.get("pinpai").toString().equals(mmpinpai)&&
                                                jieguo.get("result").toString().equals("1"))
                                        .collect(Collectors.toList());
                                String mmPiaoShu="";
                                String mmyijian="";
                                if(mmAgreeList==null){
                                    mmPiaoShu="0／"+String.valueOf(mmUserList.size());
                                    mmyijian="不通过";
                                }else {
                                    mmPiaoShu=String.valueOf(mmAgreeList.size())+"／"+String.valueOf(mmUserList.size());
                                    if(mmAgreeList.size()>=mmPassNumber){
                                        mmyijian="通过";
                                    }else{
                                        mmyijian="不通过";
                                    }
                                }

                                //一个品种最多通过两个品牌
                                if(mmPinpaiFenlei.size()>2){
                                    if("通过".equals(mmyijian)){
                                        List<Map<String, Object>> mmAgreeNumList=ddService.getAgreeListByXiangMuIdAndPinZhong(ppxiangmuid,Integer.valueOf(mmPinzhong));
                                        if(mmAgreeNumList.size()>2){
                                           int agreenumber=Integer.valueOf(mmAgreeNumList.get(1).get("agreenumber").toString());
                                           if(mmAgreeList.size()<agreenumber){
                                               mmyijian="不通过";
                                           }
                                        }
                                    }
                                }


                                wsheet.addCell(new Label(0, mmxinghaoKaishihang,mmXuhao,wcfFC));
                                wsheet.addCell(new Label(3, mmxinghaoKaishihang, mmxinghaochaifen,wcfFC));
                                wsheet.addCell(new Label(4, mmxinghaoKaishihang,mmpingtaibianma,wcfFC));
                                wsheet.addCell(new Label(5, mmxinghaoKaishihang,mmzhucezhengmingcheng,wcfFC));
                                wsheet.addCell(new Label(6, mmxinghaoKaishihang,mmguige,wcfFC));
                                wsheet.addCell(new Label(7, mmxinghaoKaishihang,mmxinghao,wcfFC));
                                wsheet.addCell(new Label(8, mmxinghaoKaishihang,mmzhucezhenghao,wcfFC));
                                wsheet.addCell(new Label(9, mmxinghaoKaishihang,mmshengchanshang,wcfFC));
                                wsheet.addCell(new Label(10, mmxinghaoKaishihang,mmjijiadanwei,wcfFC));
                                wsheet.addCell(new Label(11, mmxinghaoKaishihang,mmpingtaiqingkuang,wcfFC));
                                wsheet.addCell(new Label(12, mmxinghaoKaishihang,mmpingtaidanjia,wcfFC));

                                wsheet.addCell(new Label(13, mmxinghaoKaishihang,mmPiaoShu,wcfFC));
                                wsheet.addCell(new Label(14, mmxinghaoKaishihang,mmyijian,wcfFC));
                                mmxinghaoKaishihang = mmxinghaoKaishihang+1;
                            }
                        }
                        wsheet.addCell(new Label(2, mmpinpaiKaishihang, mmPinpaiOne,wcfFC));
                        wsheet.mergeCells(2, mmpinpaiKaishihang, 2, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        wsheet.mergeCells(13, mmpinpaiKaishihang, 13, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        wsheet.mergeCells(14, mmpinpaiKaishihang, 14, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        mmpinpaiKaishihang = mmpinpaiKaishihang+mmPinpaiHebingshu+1;
                    }
                }
                wsheet.addCell(new Label(1, mmpinzhongKaishihang, mmPinzhongOne,wcfFC));
                wsheet.mergeCells(1, mmpinzhongKaishihang, 1, mmpinzhongKaishihang+mmHebingshu);
                mmpinzhongKaishihang = mmpinzhongKaishihang+mmHebingshu+1;

            }
            wbook.write(); // 写入文件
            wbook.close();


        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }





    // 导出某个人员的签字表
    @RequestMapping("qianzibiao")
    public void qianzibiao(HttpServletRequest request, HttpServletResponse response,
                             @RequestParam("xiangmuid") String ppxiangmuid,
                             @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                             @RequestParam("userid") String ppuserid) {
        try {
            ddService.daochuqianzibiao(ppxiangmuid, ppuserid, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("lingdaohuibaobiao")
    public void lingdaohuibaobiao(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam("xiangmuid") String ppxiangmuid,
                           @RequestParam("xiangmuleixing") String ppxiangmuleixing) {
       // List<Map<String,Object>> mmJieGuoList=ddService.findByxiangmuId(ppxiangmuid);
        List<List<String>> mmResultList = new ArrayList<List<String>>();
        List<YltpRenyuanfenpei> mmUserList=ddYltprenyuanfenpeiService.findUserByXiangmuId(ppxiangmuid);
        int mmWeiyuanshu=mmUserList.size();
        for(int i=0;i<mmUserList.size();i++){
            Integer mmSubmit = mmUserList.get(i).getIssubmit();
            if(mmSubmit==0){
                error("该项目有委员没有提交结果表",response);
                return;
            }
        }
        double mmNumber=Math.ceil((double) mmUserList.size()*((double)2/(double)3));
        int mmPassNumber=(int)mmNumber;
        List<Map<String,Object>> mmList = ddHaocaiyinjinService.findDaochuPinzhongAll(ppxiangmuid);
        int mmPinzhongShu=mmList.size();
        List<Haocaiyinjin> mmHaocaiyinjinList=ddHaocaiyinjinService.findAllByXiangmuId(ppxiangmuid);
        int mmPingGui=mmHaocaiyinjinList.size();

        int mmPassPingGui=0;
        int mmNoPassPingGui=0;

        List<Map<String,Object>> mmPinguiList=ddHaocaiyinjinService.findPinguiResult(ppxiangmuid);
        for(Haocaiyinjin mmHaocaiyinjin : mmHaocaiyinjinList){

            List<Map<String, Object>> mmAgreeList=mmPinguiList.stream()
                    .filter(pingui -> pingui.get("id").toString()
                            .equals(mmHaocaiyinjin.getId()) &&
                            pingui.get("result").toString().equals("1"))
                    .collect(Collectors.toList());
            if(mmAgreeList==null){
                mmNoPassPingGui=mmPingGui;
            }else {

                if(mmAgreeList.size()>=mmPassNumber){
                    mmPassPingGui=mmPassPingGui+1;
                }else{
                    mmNoPassPingGui=mmNoPassPingGui+1;
                }
            }
        }
        String mmMeg="今天共计"+mmWeiyuanshu+"个委员参与医用耗材引进表决，同意数在"+mmPassNumber+"票及以上，最终结果为通过。医用耗材投票共计"
                +mmPinzhongShu+"个品种，涉及"+mmPingGui+"个品规，其中"+mmPassPingGui+"个品规同意数在"+mmPassNumber+"票及以上，最终通过；"
                +mmNoPassPingGui+"个品规同意数不足"+mmPassNumber+"票，最终结果不通过。";
       /* List<String> mmTitleList = new ArrayList<String>();
        mmTitleList.add(mmMeg);
        mmResultList.add(mmTitleList);
        try {
            //取出文档存放的根目录，并去除文件最后的斜杠
            if (userfilepath.endsWith("/")) {
                userfilepath = userfilepath.substring(0, userfilepath.length()-1);
            }

            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd");
            String mmSavePath = userfilepath +  "/temp/tiwaijieguo/"+mmDateFormat.format(new Date()) + "/";

            CreateXLS.createExcel(mmResultList, mmSavePath,  "hbb"+ UUID.randomUUID().toString()+".docx",
                    "hbb", request, response);
            return "/ylsbtp/lingdaohuibaobiao.docx";

        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.getMessage();
        }*/

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        OutputStream os=null;
        WritableWorkbook wbook=null;

        try{
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            String wenjiantitle = "耗材表决结果汇报表"+ StringHandle.tiemString(); //文件名
            String tmptitle = "医用耗材引进表决结果汇报表"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC);
            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,500); //第一行高度
            wsheet.mergeCells(0, 0, 10, 0);  //合并单元格
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,15); //宽度
            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            wsheet.addCell(new Label(0, 1, mmMeg,wcfFC));
            wsheet.mergeCells(0, 1, 10, 6);
            wbook.write(); // 写入文件
            wbook.close();


        }catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    public void error(String string,HttpServletResponse mmResponse) {
        try {
            mmResponse.setCharacterEncoding("gbk");
            PrintWriter mmOut;
            mmOut = mmResponse.getWriter();
            mmOut.print("<div style='text-align:center;font-size:18px; font-weight:weight;'>"+string+"</div>");
            mmOut.flush();
            mmOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
